﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BTaskCmd
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/6 15:05:46
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion


namespace KingerRobot.MexicoSailunMDC.SpiService;
internal class BTaskCmd : ITaskCmd
{
    private readonly static ITask _Task = new BTask();
    private readonly static IWcsEquipLoc _WcsEquipLoc = new BWcsEquipLoc();
    public POWcsTaskCmd GetTaskCmdByTaskId(long taskId)
    {
        var db = Db.Default;
        //WriteLog($"线程{Thread.CurrentThread.ManagedThreadId}开始执行,任务{taskId}GetTaskCmdByTaskId30,ContextId-{db.ContextId}", true);
        var result = db.WcsTaskCmds
            .Where(t => t.TaskId == taskId).OrderByDescending(t => t.CreateTime)
            .FirstOrDefault();
        //WriteLog($"线程{Thread.CurrentThread.ManagedThreadId}结束执行,任务{taskId}GetTaskCmdByTaskId34,ContextId-{db.ContextId}", true);
        return result;
    }

    public POWcsTaskCmd GetTaskCmdByStartLocAndCode(long startLoc, int taskNo)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => t.TaskNo == taskNo && t.StartLoc == startLoc)
            .FirstOrDefault();
        return result;
    }
    public POWcsTaskCmd GetTaskCmdById(long cmdId)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => t.Id == cmdId).AsNoTracking()
            .FirstOrDefault();
        return result;
    }
    public int UpdateOutCmdStatus()
    {
        var db = Db.Default;
        var result = 0;
        //var outTasks = db.WcsTasks.Where(t => t.TaskType == (int)EnumTaskType.Exwarehouse && t.TaskStatus == (int)EnumTaskStatus.Executable).ToList();
        //foreach (var outTask in outTasks)
        //{
        //    var count = db.WcsTaskCmds.Where(t => t.TaskId == outTask.Id && t.CmdStatus == (int)EnumTaskCmdStatus.Create).Count();
        //    if (count > 0)
        //    {
        //        result += db.WcsTaskCmds.Where(t => t.TaskId == outTask.Id && t.CmdStatus == (int)EnumTaskCmdStatus.Create)
        //            .Update( new POWcsTaskCmd
        //            {
        //                CmdStatus = (int)EnumTaskCmdStatus.Executable,
        //                RemarkMemo = "定时器修改任务状态为可执行",
        //                //UpdateTime = DateTime.Now,
        //            });
        //    }
        //}

        var cmdList = db.WcsTasks.Join(db.WcsTaskCmds, f => f.Id, s => s.TaskId, (f, s) => new
        {
            CmdId = s.Id,
            s.CmdStatus,
            f.TaskType,
            f.TaskStatus,
        }).Where(t => t.TaskType == (int)EnumTaskType.Exwarehouse && t.TaskStatus == (int)EnumTaskStatus.Executable && t.CmdStatus == (int)EnumTaskCmdStatus.Create).Select(t => t.CmdId).ToList();
        foreach (var cmdIds in cmdList)
        {
            result += db.WcsTaskCmds.Where(t => t.Id == cmdIds)
                       .Update( new POWcsTaskCmd
                       {
                           CmdStatus = (int)EnumTaskCmdStatus.Executable,
                           RemarkMemo = "定时器修改任务状态为可执行",
                           UpdateTime = DateTime.Now,
                       });
            db.SaveChanges();
        }


        var inTasks = db.WcsTaskCmds.Where(t => t.CmdStatus == (int)EnumTaskCmdStatus.Create && t.CmdType == (int)EnumTaskType.Warehousing).ToList();
        inTasks = inTasks.Where(t => (DateTime.Now - (t.CreateTime ?? DateTime.Now)).TotalMinutes >= 10).ToList();
        //WriteLog($"定时器修改入库任务列表{inTasks.ToJson()}", true);
        foreach (var inTask in inTasks)
        {

            result += db.WcsTasks.Where(t => t.Id == inTask.Id && t.TaskStatus == (int)EnumTaskCmdStatus.Create).Update(new POWcsTask
            {
                TaskStatus = (int)EnumTaskCmdStatus.Executable,
                RemarkMemo = "定时器修改任务状态为可执行",
            });
            result += db.WcsTaskCmds.Where(t => t.Id == inTask.Id).Update(new POWcsTaskCmd
            {
                CmdStatus = (int)EnumTaskCmdStatus.Executable,
                RemarkMemo = "定时器修改任务状态为可执行",
            });
            db.SaveChanges();
        }
        return 1;

    }

    public POWcsTaskCmd GetTaskCmdByEquipId(long equipId)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => t.EquipId == equipId)
            .OrderByDescending(t => t.CreateTime)
            .FirstOrDefault();
        return result;
    }
    //public POWcsTaskCmd GetExecutableTaskCmdByEquipId(long equipId)
    //{
    //    var db = Db.Default;
    //    var result = db.WcsTaskCmds
    //        .Where(t => t.EquipId == equipId&&t.CmdStatus==(int)EnumTaskStatus.Executable)
    //        .OrderByDescending(t => t.CreateTime)
    //        .FirstOrDefault();
    //    return result;
    //}
    public POWcsTaskCmd GetExecutableTaskCmdByEquipId(long equipId)
    {
        var result = GetTaskCmdsByEquipId(equipId);
        if (result.Count == 0)
        {
            return null;
        }
        return result.Where(t => t.CmdStatus < (int)EnumTaskCmdStatus.Executed).FirstOrDefault();
    }

    public List<POWcsTaskCmd> GetExecutableTaskCmds()
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => t.CmdStatus < (int)EnumTaskCmdStatus.Executed)
            .ToList();
        return result;
    }

    private List<POWcsTaskCmd> GetTaskCmdsByEquipId(long equipId)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => t.EquipId == equipId)
            .ToList();
        return result;
    }

    public POWcsTaskCmd GetExecutedTaskCmdByEquipId(long equipId)
    {
        var result = GetTaskCmdsByEquipId(equipId);
        if (result.Count == 0)
        {
            return null;
        }

        return result.Where(t => t.CmdStatus >= (int)EnumTaskCmdStatus.Executed).FirstOrDefault();
    }


    public bool IsExecutedTaskCmdByEquipIds(List<long> equipIds)
    {
        var result = GetExecutedTaskCmdByEquipIds(equipIds).Any();
        return result;
    }

    public List<POWcsTaskCmd> GetExecutedTaskCmdByEquipIds(List<long> equipIds)
    {
        var db = Db.Default;
        var cmds = db.WcsTaskCmds
            .Where(t => t.CmdStatus >= (int)EnumTaskCmdStatus.Executed).ToList();
        var result = cmds.Join(equipIds, f => f.EquipId, s => s, (f, s) => f).ToList();

        return result;
    }

    public POWcsTaskCmd GetTaskCmdByTaskNo(int taskNo)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => t.TaskNo == taskNo)
            .FirstOrDefault();
        return result;
    }

    public List<POWcsTaskCmd> GetTaskCmdListByTaskNo(int taskNo)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => t.TaskNo == taskNo)
            .ToList();
        return result;
    }

    public POWcsTaskCmd GetExecutableTaskCmdByTaskNo(int taskNo)
    {
        var result = GetTaskCmdByTaskNo(taskNo);
        //var result = GetTaskCmdListByTaskNo(taskNo).Where(t=>t.CmdType==(int)EnumTaskCmdStatus.Executable).FirstOrDefault();
        if (result is null)
        {
            return null;
        }
        return result.CmdStatus < (int)EnumTaskCmdStatus.Executed ? result : null;
    }

    public POWcsTaskCmd GetEmsExecutableTaskCmdByTaskNo(int taskNo)
    {
        //var result = GetTaskCmdByTaskNo(taskNo);
        var result = GetTaskCmdListByTaskNo(taskNo).Where(t => t.CmdStatus == (int)EnumTaskCmdStatus.Executable).FirstOrDefault();
        if (result is null)
        {
            return null;
        }
        return result.CmdStatus < (int)EnumTaskCmdStatus.Executed ? result : null;
    }

    public POWcsTaskCmd GetExecutedTaskCmdByTaskNo(int taskNo)
    {
        var result = GetTaskCmdByTaskNo(taskNo);
        return result?.CmdStatus < (int)EnumTaskCmdStatus.Finish ? result : null;
    }
    public int IsSprayByTaskNo(int taskNo) => GetTaskCmdByTaskNo(taskNo)?.IsSpray ?? 0;


    public List<TaskInfoBean> GetTaskCmdByType(int type)
    {
        var db = Db.Default;

        var result = db.WcsTaskCmds.Join(db.WcsTasks, f => f.TaskId, s => s.Id, (f, s) => new TaskInfoBean
        {
            POWcsTaskCmd = f,
            POWcsTask = s,
        }).Where(t => t.POWcsTaskCmd.CmdType == type)
           .OrderBy(t => t.POWcsTaskCmd.CreateTime).ToList();
        return result;
    }


    //public List<TaskInfoBean> GetTaskCmdByType(int type)
    //{
    //    var db = Db.Default;
    //    var result = db.WcsTaskCmds.Join(db.WcsTasks, f => f.TaskId, s => s.Id, (f, s) => new TaskInfoBean
    //    {
    //        POWcsTaskCmd = f,
    //        POWcsTask = s,
    //    })
    //        .Where(t => t.POWcsTaskCmd.CmdType == type)
    //        .OrderBy(t => t.POWcsTaskCmd.CreateTime)
    //        .ToList();
    //    return result;
    //}

    ////OLDTODO 【已解决】分成入库出库两个函数调用， 添加一个状态判断 < 2 [可执行]
    //public List<TaskInfoBean> GetIOExecutableWcsTaskCmdsByType(EnumTaskType enumTaskType) => enumTaskType switch
    //{   //下面有改动 lsq
    //    EnumTaskType.Warehousing => GetTaskCmdByType((int)EnumTaskType.Warehousing).Where(t => t.POWcsTaskCmd.CmdStatus == (int)EnumTaskCmdStatus.Executable).ToList(),
    //    EnumTaskType.Exwarehouse => GetTaskCmdByType((int)EnumTaskType.Exwarehouse).Where(t => t.POWcsTaskCmd.CmdStatus == (int)EnumTaskCmdStatus.Executable).ToList(),
    //    _ => new List<TaskInfoBean>(),
    //};
    public List<TaskInfoBean> GetIOExecutableWcsTaskCmdsByType(EnumTaskType enumTaskType) => enumTaskType switch
    {   //下面有改动 lsq
        EnumTaskType.Warehousing => GetTaskCmdByType((int)EnumTaskType.Warehousing).Where(t => t.POWcsTaskCmd.CmdStatus == (int)EnumTaskCmdStatus.Executable).ToList(),
        EnumTaskType.Exwarehouse => GetTaskCmdByType((int)EnumTaskType.Exwarehouse).Where(t => t.POWcsTaskCmd.CmdStatus == (int)EnumTaskCmdStatus.Executable).ToList(),
        _ => new List<TaskInfoBean>(),
    };


    //给生成入库指令用
    public List<TaskInfoBean> GetWarehouingIOExecutableWcsTaskCmdsByType(EnumTaskType enumTaskType) => enumTaskType switch
    {   //下面有改动 lsq
        EnumTaskType.Warehousing => GetTaskCmdByType((int)EnumTaskType.Warehousing).Where(t => t.POWcsTaskCmd.CmdStatus == (int)EnumTaskCmdStatus.Executable && t.POWcsTask.TaskType == (int)EnumTaskType.Warehousing && t.POWcsTask.TaskStatus == (int)EnumTaskStatus.Executable).ToList(),
        EnumTaskType.Exwarehouse => GetTaskCmdByType((int)EnumTaskType.Exwarehouse).Where(t => t.POWcsTaskCmd.CmdStatus == (int)EnumTaskCmdStatus.Executable).ToList(),
        _ => new List<TaskInfoBean>(),
    };

    /// <summary>
    /// 开始位置获取所有任务
    /// </summary>
    /// <param name="locId"></param>
    /// <returns></returns>
    private List<POWcsTaskCmd> GetTaskCmdByStartLocId(long locId)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => t.StartLoc == locId)
            .ToList();
        return result;
    }

    /// <summary>
    /// 目标位置获取所有任务
    /// </summary>
    /// <param name="locId"></param>
    /// <returns></returns>
    public List<POWcsTaskCmd> GetTaskCmdByTargetLocId(long locId)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => t.TargetLoc == locId)
            .OrderBy(t => t.CreateTime)
            .ToList();
        return result;
    }

    public POWcsTaskCmd GetNextTaskCmdByLocId(long locId)
    {
        // 查找时间最早
        var result = GetTaskCmdByStartLocId(locId).OrderBy(t => t.CreateTime).FirstOrDefault();
        return result;
    }
    public POWcsTaskCmd GetNextTaskCmdByScanLocId(long scanLocId)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds.Join(db.WcsTasks, f => f.TaskId, s => s.Id, (f, s) => new
        {
            TaskCmd = f,
            Task = s,
        })
            .Where(t => t.Task.SpinScanId == scanLocId).OrderBy(t => t.Task.SpinTime)
            .Select(t => t.TaskCmd)
            .FirstOrDefault();
        return result;
    }
    public POWcsTaskCmd GetNextTaskCmdByScanLocId(long scanLocId, long emsLoc, DateTime scannerTime) 
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds.Join(db.WcsTasks, f => f.TaskId, s => s.Id, (f, s) => new
        {
            TaskCmd = f,
            Task = s,
        })
            .Where(t => t.Task.SpinScanId == scanLocId&&t.Task.SpinTime> scannerTime&&t.TaskCmd.TargetLoc==emsLoc).OrderBy(t => t.Task.SpinTime)
            .Select(t => t.TaskCmd)
            .FirstOrDefault();
        return result;
    }
    public POWcsTaskCmd GetUpTaskCmdByLocId(long locId)
    {
        // 查找时间最早
        var result = GetTaskCmdByStartLocId(locId).OrderByDescending(t => t.CreateTime).FirstOrDefault();
        return result;
    }

    //public POWcsTaskCmd GetNextExecutableTaskCmdByLocId(long locId)
    //{
    //    var result = GetTaskCmdByStartLocId(locId);
    //    return result.Where(t => t.CmdStatus < (int)EnumTaskCmdStatus.Executed).OrderBy(t => t.CreateTime).FirstOrDefault();
    //}
    private POWcsTaskCmd GetExecutableTaskCmdByStartLocId(long locId)
    {
        var result = GetTaskCmdByStartLocId(locId);
        return result.Where(t => t.CmdStatus < (int)EnumTaskCmdStatus.Executed).FirstOrDefault();
    }
    public long GetExecutableTaskIdkByStartLocId(long locId) => GetExecutableTaskCmdByStartLocId(locId)?.TaskId ?? 0;

    public int GetTaskCmdCountByLocs(List<long> locIds)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => locIds.Contains(t.TargetLoc.GetValueOrDefault()))
            .Count();
        return result;
    }

    private POWcsTaskCmd GetOutPutExecutableTaskCmdByLocId(long locId, long taskId)
    {
        var result = GetTaskCmdByStartLocId(locId);
        return result.Where(t => t.TaskId == taskId && t.CmdStatus < (int)EnumTaskCmdStatus.Executed).FirstOrDefault();
    }

    public long GetOutPutExecutableTaskIdkByLocId(long locId, long taskId) => GetOutPutExecutableTaskCmdByLocId(locId, taskId)?.TaskId ?? 0;

    public POWcsTaskCmd GetTaskCmdByProductCode(string productCode)
    {
        var task = _Task.GetTaskByProductCode(productCode);
        if (task == null)
        {
            return null;
        }
        return GetTaskCmdByTaskId(task.Id.GetValueOrDefault());
    }

    public long GetTaskCmdIdByProductCode(string productCode) => GetTaskCmdByProductCode(productCode)?.Id ?? 0;

    public POWcsTaskCmd CreateTaskCmd(POWcsTaskCmd pOWcsTaskCmd)
    {
        if (pOWcsTaskCmd.Id.GetValueOrDefault() == 0)
        {
            pOWcsTaskCmd.Id = XID.Nextval;
        }

        if ((pOWcsTaskCmd.TaskId == 0 || pOWcsTaskCmd.TaskId is null) && pOWcsTaskCmd.TaskNo != 0)
        {
            pOWcsTaskCmd.TaskId = _Task.GetTaskByTaskNo(pOWcsTaskCmd.TaskNo.GetValueOrDefault())?.Id;
        }

        if (pOWcsTaskCmd.TaskId == 0
            || pOWcsTaskCmd.StartLoc == 0
            || pOWcsTaskCmd.TargetLoc == 0)
        {
            return null;
        }

        if (pOWcsTaskCmd.EquipId == 0 || pOWcsTaskCmd.EquipId is null)
        {
            // 设备
            var equipId = _WcsEquipLoc.GetEquipIdByLocId(pOWcsTaskCmd.StartLoc.GetValueOrDefault());
            pOWcsTaskCmd.EquipId = equipId;
        }
        var db = Db.Default;
        var num = db.WcsTaskCmds.Insert(pOWcsTaskCmd);
        if (num == 0)
        {
            return null;
        }
        return pOWcsTaskCmd;
    }
    public int BakTaskCmdByTaskId(long taskId)
    {

        var taskCmd = GetTaskCmdByTaskId(taskId);
        if (taskCmd is null)
        {
            return 0;
        }
        var db = Db.Default;
        var result = 0;
        try
        {
            result = db.BakWcsTaskCmds.CopyFrom(taskCmd);
        }
        catch (Exception ex)
        {
            WriteLog(ex.ToString());
        }

        return result;

    }
    public int DeleteTaskCmdByTaskId(long taskId)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => t.TaskId == taskId).Delete();

        return result;
    }
    public int BakAndDeleteCmd(long taskId)
    {
        var db = Db.Default;
        var cmds = db.WcsTaskCmds
            .Where(t => t.TaskId == taskId)
            .ToList();
        var result = 0;
        foreach (var cmd in cmds)
        {
            //if (cmd.CmdStatus < (int)EnumTaskCmdStatus.Executed)
            //{
            //    continue;
            //}
            try { db.BakWcsTaskCmds.CopyFrom(cmd); } catch (Exception ex) { WriteLog(ex.ToString()); };
            result += db.WcsTaskCmds.Where(r => r.Id == cmd.Id).Delete();
        }

        return result;
    }
    public int UpdateCmdStatusByTaskIdAndType(long taskId, int status, int type)
    {
        var db = Db.Default;

         db.WcsTaskCmds.Where(f => f.TaskId == taskId && f.CmdType == type).Update( new POWcsTaskCmd { CmdStatus = status });
        return db.SaveChanges();
    }
    public int UpdateTaskCmdStatus(long locId, long taskId, int status)
    {

        var taskCmd = GetTaskCmdByTaskId(taskId);
        if (taskCmd is null)
        {
            return 0;
        }

        var bean = new POWcsTaskCmd
        {
            CmdStatus = status,
            UpdateTime = DateTime.Now,
        };
        if (status == (int)EnumTaskCmdStatus.Executed)
        {
            bean.ExecuteTime = DateTime.Now;
        }

        long? finishLoc = null;
        int? finishMode = (int)EnumTaskStatus.Create;
        DateTime? finishTime = null;
        //添加了入库完成验证 lsq
        if (status == (int)EnumTaskStatus.Finish)
        {
            // 完成时修改
            finishLoc = locId;
            finishMode = (int)EnumFinishMode.NormalEnd;
            finishTime = DateTime.Now;

            bean = new POWcsTaskCmd
            {
                CmdStatus = status,
                FinishLoc = finishLoc,
                FinishMode = finishMode,
                FinishTime = finishTime
            };
        }

        var db = Db.Default;

        return db.WcsTaskCmds
            .Where(t => t.Id == taskCmd.Id)
            .Update(bean);
    }
    public int UpdateTaskCmdStatusByTaskIds(List<long> taskIds, int status)
    {
        long? finishLoc = null;
        int? finishMode = (int)EnumTaskStatus.Create;
        DateTime? finishTime = null;

        var db = Db.Default;

         db.WcsTaskCmds
            .Where(t => taskIds.Contains(t.TaskId.GetValueOrDefault()))
            .Update( new POWcsTaskCmd
            {
                CmdStatus = status,
                UpdateTime = DateTime.Now,
                FinishLoc = finishLoc,
                FinishMode = finishMode,
                FinishTime = finishTime,
                //RemarkMemo = "修改任务状态为可执行",
                //ExecuteTime= DateTime.Now,
            });
        return db.SaveChanges();
    }


    public bool IsTaskCmdByEquipId(long equipId) => GetTaskCmdByEquipId(equipId) is not null;
    public int UpdateTaskCmdPairWait(long equipId, int wait)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds
            .Where(t => t.EquipId == equipId)
            .Update( new POWcsTaskCmd
            {
                PairWait = wait,
            });

        return db.SaveChanges();
    }

    public int UpdateCmdTargetLoc(long id, long targetLoc)
    {
        var db = Db.Default;
        var num = db.WcsTaskCmds.Where(t => t.TaskId == id).Update( new POWcsTaskCmd
        {
            TargetLoc = targetLoc,
            UpdateTime = DateTime.Now
        });

        return db.SaveChanges();
    }

    public int UpdateCmdStartLoc(long id, long startLoc)
    {
        var db = Db.Default;
        var num = db.WcsTaskCmds.Where(t => t.TaskId == id).Update( new POWcsTaskCmd
        {
            StartLoc = startLoc,
            UpdateTime = DateTime.Now
        });

        return db.SaveChanges();
    }

    public POWcsTaskCmd GetNextTaskCmdByPairWait(long startLoc, int pairWait)
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds.Where(t => t.CmdStatus != (int)EnumTaskCmdStatus.Create && t.StartLoc == startLoc && t.PairWait == 2).OrderByDescending(t => t.CreateTime).FirstOrDefault();
        return result;
    }

    public List<TaskGroupCount> GetTargetLocTaskGroup()
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds.GroupBy(t => t.TargetLoc)
            .Select(t => new TaskGroupCount
            {
                LocId = t.Key.Value,
                Count = t.Count()
            }).ToList();
        return result;
    }
    public List<TaskGroupCount> GetExecutableTargetLocTaskGroup()
    {
        var db = Db.Default;

        var result = db.WcsTaskCmds.Where(t => t.CmdStatus == (int)EnumTaskCmdStatus.Executable).GroupBy(t => t.TargetLoc)
            .Select(t => new TaskGroupCount
            {
                LocId = t.Key.Value,
                Count = t.Count()
            }).ToList();
        return result;
    }


    public List<TaskGroupCount> GetStartLocTaskGroup()
    {
        var db = Db.Default;
        var result = db.WcsTaskCmds.GroupBy(t => t.StartLoc)
            .Select(t => new TaskGroupCount
            {
                LocId = t.Key.Value,
                Count = t.Count()
            }).ToList();
        return result;
    }

    public List<TaskGroupCount> GetStartLocTaskGroupContain(List<long> taskIds)
    {
        // 优化 1
        var db = Db.Default;
        var taskCmds = db.WcsTaskCmds.ToList();

        var result = taskCmds
            .Where(s => taskIds.Contains(s.TaskId.GetValueOrDefault())).GroupBy(t => t.StartLoc)
            .Select(t => new TaskGroupCount
            {
                LocId = t.Key.Value,
                Count = t.Count()
            }).Where(t => t.Count > 0).ToList();
        return result;
    }

}